﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of TransportSupport class
	/// Additional functions to manipulate transport objects
	/// </summary>
	public static class TransportSupport
	{

		/////////////////////////////////////////////////////////////
		#region Static Operations - Contacts

		public static string AddressToLabelContent( ContactAddressTransport address )
		{

			// Load string array
			List<string> stringList = _addressToStringList(address);

			// Set content
			StringBuilder sb = new StringBuilder();
			for (int i2 = 0; i2 < stringList.Count; i2++)
			{
				if (i2 > 0)
					sb.Append("\n");
				sb.Append(stringList[i2]);
			}

			// Return string
			return sb.ToString();

		}

		public static void AddressToTextBlockContent(ContactAddressTransport address, TextBlock tbAddress)
		{

			// Load string array
			List<string> stringList = _addressToStringList(address);

			// Set text-block
			_smartStuffTextBlock(tbAddress,stringList);

		}

		private static void _smartStuffTextBlock(TextBlock tbAddress, List<string> stringList)
		{

			// Load the inlines
			int iInlineOffset = 0;
			for (int i1 = 0; i1 < stringList.Count; i1++)
			{

				// If need to add newline...
				if(i1 > 0)
				{
					// Set the new line
					if(tbAddress.Inlines.Count <= iInlineOffset)
						tbAddress.Inlines.Add( new LineBreak() );
					else if(!(tbAddress.Inlines[iInlineOffset] is LineBreak))
						tbAddress.Inlines.Insert(iInlineOffset,new LineBreak());
					iInlineOffset++;
				}

				// Set the run
				if (tbAddress.Inlines.Count <= iInlineOffset)
					tbAddress.Inlines.Add(new Run());
				else if (!(tbAddress.Inlines[iInlineOffset] is Run))
					tbAddress.Inlines.Insert(iInlineOffset, new Run());
				(tbAddress.Inlines[iInlineOffset] as Run).Text = stringList[i1];
				iInlineOffset++;

			}

			// Trim overhand
			while (tbAddress.Inlines.Count > iInlineOffset)
				tbAddress.Inlines.RemoveAt(tbAddress.Inlines.Count - 1);			

		}

		private static List<string> _addressToStringList(ContactAddressTransport address)
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Load string array
			if (!string.IsNullOrEmpty(address.AddressLineA))
				Ftmp.Add(address.AddressLineA);
			if (!string.IsNullOrEmpty(address.AddressLineB))
				Ftmp.Add(address.AddressLineB);
			if (!string.IsNullOrEmpty(address.AddressLineC))
				Ftmp.Add(address.AddressLineC);
			if (!string.IsNullOrEmpty(address.City))
				Ftmp.Add(address.City);
			if (!string.IsNullOrEmpty(address.County))
				Ftmp.Add(address.County);
			if (!string.IsNullOrEmpty(address.Postcode))
				Ftmp.Add(address.Postcode);
			if (!string.IsNullOrEmpty(address.CountryCode))
			{
				if (CachedDataBase.SingletonBase.CountryMap.ContainsKey(address.CountryCode))
					Ftmp.Add(CachedDataBase.SingletonBase.CountryMap[address.CountryCode]);
				else
					Ftmp.Add(address.CountryCode);
			}

			// Return string
			return Ftmp;

		}

		public static string ComposeFullContactName(ContactTransport contactTransport)
		{

			// Build string
			StringBuilder sb = new StringBuilder();
			sb.Append(contactTransport.Title);
			sb.Append(" ");
			sb.Append(contactTransport.FirstName);
			sb.Append(" ");
			sb.Append(contactTransport.Initials);
			sb.Append(" ");
			sb.Append(contactTransport.Surname);
			sb.Append(" ");
			if (!string.IsNullOrEmpty(contactTransport.Abbr))
				sb.AppendFormat(" aka \"{0}\"", contactTransport.Abbr);
			sb.Append(" ");
			if (!string.IsNullOrEmpty(contactTransport.Position))
				sb.AppendFormat(" - {0}", contactTransport.Position);
			sb = sb.Replace("  ", " ");

			// If there is no contact name...
			string strContactName = sb.ToString().Trim();
			if (strContactName.Length == 0)
				strContactName = "[no name]";

			// Return string
			return sb.ToString();

		}

		#endregion Static Operations - Contacts

		/////////////////////////////////////////////////////////////
		#region Static Operations - Comms Target

		public static void LoadCommTargetTypeControl(ComboBox ctrl, Definitions.eCommTargetType select)
		{

			// Add to control
			ctrl.Items.Add(new ComboBoxObject("Debtor", (int)Definitions.eCommTargetType.Debtor, null));
			ctrl.Items.Add(new ComboBoxObject("Client", (int)Definitions.eCommTargetType.Client, null));
			ctrl.Items.Add(new ComboBoxObject("Agent", (int)Definitions.eCommTargetType.Agent, null));
			ctrl.Items.Add(new ComboBoxObject("Solicitor", (int)Definitions.eCommTargetType.Solicitor, null));

			// Select item
			if (select != Definitions.eCommTargetType.NotSet)
				ComboBoxObject.SelectByItemData(ctrl, (int)select);

		}

		#endregion Static Operations - Comms Target

		/////////////////////////////////////////////////////////////
		#region Static Operations - Account

		public static Money CalcChasingDebt( this AccountTransport account )
		{

			// Calculate account and chasable total
			List<Money> chasableMoneyList = new List<Money>();
			foreach (AccountInvoiceTransport inv in account.AccountInvoices)
			{
				if (true)
					chasableMoneyList.Add(new Money(inv.Amount.Amount, inv.Amount.CurrencyCode));
			}

			// Return variable
			return Money.Add(chasableMoneyList);

		}

		#endregion Static Operations - Account

	}

}
